﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Markup;
using System.Xml.Linq;
using System.Windows.Controls;

namespace ViewMaker.Core.Tests
{
    public static class TestHelper
    {
        public static XamlNode CreateTestParent()
        {
            var ele = XElement.Parse(
      @"<ContentControl 
            xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
            xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""                                  
            xmlns:sdk=""http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"" 
            xmlns:vg=""ViewMaker.Core.Silverlight;assembly=SilverlightViewMaker.Core""
            xmlns:i=""http://schemas.microsoft.com/expression/2010/interactivity""
            xmlns:it=""clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit""
            xmlns:input=""clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input""
            xmlns:toolkit=""clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit""
            IsTabStop=""False""
        > 
            <ContentControl.Resources>
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source=""/SilverlightViewMaker.Core;component/silverlight/slgenericresource.xaml""/>
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </ContentControl.Resources>  
        </ContentControl>");

            return new XamlNode(ele);
        }

        public static T GetFrameworkElement<T>(string xaml)
        {
            return (T)XamlReader.Load(xaml);
        }
        public static T GetFrameworkElement<T>(XamlNode xaml)
        {
            var root = CreateTestParent();
            root.AppendChild(xaml);
            var rootXaml = GetFrameworkElement<ContentControl>(GetXamlCode(root));
            return (T)rootXaml.Content;
        }

        public static string GetXamlCode(XamlNode xaml)
        {
            var r = xaml.Node.CreateReader();
            r.MoveToContent();
            return r.ReadOuterXml();
        }

    }
}
